//
// Created by delin10 on 2020/5/4.
//

#include "string_utils.h"
#include "stdarg.h"
#include "memory.h"
#include "stdio.h"
#include "stdlib.h"

char *format(char *format, ...)
{
    va_list list;
    //1. 先获取格式化后字符串的长度
    va_start(list, format);
    int size = vsnprintf(NULL, 0, format, list);
    va_end(list);
    if(size <= 0) {
        return format;
    }
    size++;

    //2. 复位va_list，将格式化字符串写入到buf
    va_start(list, format);
    char *buf = (char *)malloc(size);
    vsnprintf(buf, size, format, list);
    va_end(list);
    return buf;
}